package com.huawei.gzq.service;

import com.huawei.gzq.domain.User;
import com.huawei.gzq.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;

    @Override
    public User findUser(int id) {
        User user = mapper.findUser(id);
        return user == null ? User.blankUser() : user;
    }

    @Override
    public List<User> findAll() {
        return mapper.findAll();
    }

    @Override
    public String addUser(User user) {
        User oldUser = mapper.findUser(user.getId());
        if (oldUser == null) {
            int res = mapper.addUser(user);
            System.out.println(res);
            if (res == 1)
                return "Add user successfully.<br>" + user;
            else
                return "Add user failed.<br>";
        } else {
            return "The user id already exists.<br>";
        }
    }
}
